1553C - Penalty - CodeForces Solution


bitmasks brute force dp greedy *1200

Please click on ads to support us..

Python Code:

t = int(input())

def can_stop(first_score, second_score, num_kicks):
    if ((second_score < first_score) and (num_kicks % 2 == 1)):
        return abs(first_score - (second_score + 1)) > ((10 - num_kicks)//2)
    return abs(first_score - (second_score)) > ((10 - num_kicks)//2)

for i in range(t):
    s = input()
    first_score = 0
    second_score = 0

    first_case = 1
        while (first_case < 10):
        if (first_case % 2 == 1 and s[first_case-1] != '0'):
            first_score += 1
        elif (first_case % 2 == 0 and s[first_case-1] == '1'):
            second_score += 1

        if (can_stop(first_score, second_score, first_case)):
            break

        first_case += 1
    
    first_score = 0
    second_score = 0
    second_case = 1
        while (second_case < 10):
        if (second_case % 2 == 1 and s[second_case-1] == '1'):
            first_score += 1
        elif (second_case % 2 == 0 and s[second_case-1] != '0'):
            second_score += 1
     
        if (can_stop(first_score, second_score, second_case)):
            break

        second_case += 1
    
    print(min(first_case, second_case))


    

C++ Code:

#include <bits/stdc++.h>
#ifdef DEBUG
#include "./../Algorithms/competitive_programming/debug.h"
#else
#define debug(...) 42
#endif
using namespace  std;
#define ll long long

int check(string& s) {
    vector<int> a, b;
    for (int i = 0; i < 10; i += 2) {
        a.push_back(s[i] == '1');
        int n = a.size() - 1;
        if (i)a[n] += a[n - 1];
    }
    // debug(a);
    for (int i = 1; i < 10; i += 2) {
        b.push_back(s[i] == '1');
        int n = b.size() - 1;
        if (i > 1)b[n] += b[n - 1];
    }
    int ans = 10;
    for (int i = 0; i < 5; i++) {
        int a_did = a[i];
        int a_have = 5 - i - 1;
        int b_did = (i ? b[i - 1] : 0);
        int b_have = 5 - i;
        if (a_did > b_did + b_have || a_did + a_have < b_did) {
            ans = min(ans, 2 * i + 1);
        }

        b_did = b[i];
        b_have = 5 - i - 1;
        if (a_did > b_did + b_have || a_did + a_have < b_did) {
            ans = min(ans, 2 * i + 2);
        }
        // debug(i, a_did, a_have, b_did, b_have);
    }
    // debug(ans);
    return ans;
}
inline void solve() {
    string s; cin >> s;
    // a best      worst
    string s1 = s, s2 = s;
    for (int i = 0; i < 10; i++) {
        if (s[i] == '?') {
            if (i & 1) {
                s1[i] = '0';
                s2[i] = '1';
            }
            else {
                s1[i] = '1';
                s2[i] = '0';
            }
        }
    }
    // debug(s1, s2);
    cout << min(check(s1), check(s2)) << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)solve();
}


Comments

Submit
0 Comments
More Questions

1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree